<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 充值记录
 * Class RechargeController
 * @package App\Http\Controllers\Admin
 */
class RechargeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth:admin');
    }

    public function index()
    {
        $isHandle = $_GET['is_handle'] ?? 0;
        $w = [];
        if (!empty($isHandle)) {
            $w[] = ['handle_time', 'NOT', 'NULL'];
        }
        $list = DB::table('user_recharge_order')
            ->leftJoin('users','users.id', '=', 'user_recharge_order.user_id')
            ->select(['user_recharge_order.*', 'users.unionid'])
            ->where('pay_status', 1)
            ->where($w)
            ->orderBy('user_recharge_order.id', 'desc')
            ->paginate(15)
            ->withPath('?is_handle=' . @$_GET['is_handle'] .'');

        return view('admin.recharge.index',['list'=>$list]);
    }

    /**
     * 处理记录
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function handleRecharge($id)
    {
        //更新
        DB::table('user_recharge_order')
            ->where('id', $id)
            ->update([
                'handle_time' => date("Y-m-d H:i:s"),
            ]);
        return successResp('');
    }

    public function gameRechargeListPage()
    {
        return view('admin.recharge.game_recharge');
    }

    /**
     * 获取余额
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getGameRechargeList(Request $request)
    {
        $list = DB::table('game_recharge')
            ->where(function ($query) {
                if (@$_GET['unionid']) {
                    $query->where('unionid', @$_GET['unionid']);
                }
                if (@$_GET['game_num']) {
                    $query->where('game_num', @$_GET['game_num']);
                }
            })
            ->orderBy('id', 'desc')
            ->paginate(10);
        return successResp($list);
    }
}
